package com.example.boke.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.boke.entity.Blog;
import com.example.boke.entity.Type;
import com.example.boke.mapper.BlogMapper;
import com.example.boke.mapper.TypeMapper;
import com.example.boke.service.impl.TypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-05-02
 */
@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private TypeServiceImpl typeServiceimpl;

    @Autowired
    private BlogMapper blogMapper;


    @GetMapping(value = "/type")
    public String type(Integer pc,HttpSession session , Model model)
    {
        if (pc == null)
        {
            pc = 1;
        }

        long userId = (long) session.getAttribute("id");

        Page<Type> typePage = new Page<>(pc,5);

        QueryWrapper<Type> typeQueryWrapper = new QueryWrapper<Type>();

        typeQueryWrapper.eq("user_id",userId).orderByDesc("id");

        typeMapper.selectPage(typePage,typeQueryWrapper);

        model.addAttribute("typelist", typePage.getRecords());
        model.addAttribute("countpage", typePage.getPages());//总页数
        model.addAttribute("pc", typePage.getCurrent());
        model.addAttribute("hasPrevious",typePage.hasPrevious());   //是否有上一页
        model.addAttribute("hasNext",typePage.hasNext());       //是否有下一页

        return "admin/types";
    }

    @GetMapping(value = "/addtype")
    public String addtype()
    {
        return "admin/type-input";
    }

    @PostMapping(value = "/savetype")
    public String savetype(String name , HttpSession session, RedirectAttributes redirectAttributes) {

        QueryWrapper<Type> typeQueryWrapper = new QueryWrapper<>();

        long userId = (long)session.getAttribute("id");

        typeQueryWrapper.eq("name",name).eq("user_id",userId);

        Integer integer = typeMapper.selectCount(typeQueryWrapper);

        if ( integer != 0)
        {
            redirectAttributes.addFlashAttribute("message","分类已存在");
            redirectAttributes.addFlashAttribute("name",name);
            return "redirect:/admin/addtype";
        }

        Type type = new Type();

        type.setName(name);

        type.setUserId(userId);

        boolean save = typeServiceimpl.save(type);

        if (save)
        {
            redirectAttributes.addFlashAttribute("message","新增分类成功");
            return "redirect:/admin/type";

        }else {
            redirectAttributes.addFlashAttribute("message","新增分类失败");
            return "redirect:/admin/type";
        }

    }

    @GetMapping(value = "/removetype")
    public String removetype(HttpSession sessino , Long typeId,RedirectAttributes redirectAttributes)
    {
        Long UserId = (Long) sessino.getAttribute("id");

        //删除前先判断是否有博客文章引用了该分类，有引用要先删除文章才能删除分类
        QueryWrapper<Blog> blogQueryWrapper = new QueryWrapper<Blog>();
        blogQueryWrapper.eq("user_id",UserId).eq("type_id",typeId);

        Integer result = blogMapper.selectCount(blogQueryWrapper);

        if (result != 0)
        {
            redirectAttributes.addFlashAttribute("message","删除失败，有文章引用该分类");
            return "redirect:/admin/type";
        }


        UpdateWrapper<Type> typeUpdateWrapper = new UpdateWrapper<>();

        typeUpdateWrapper.eq("user_id",UserId).eq("id",typeId);

        typeServiceimpl.remove(typeUpdateWrapper);

        return "redirect:/admin/type";

    }

    @GetMapping(value = "/updatetypeone")
    public String updatetype(String typeName , Long typeId,RedirectAttributes redirectAttributes)
    {

        System.out.println(typeName);
        redirectAttributes.addFlashAttribute("name",typeName);
        redirectAttributes.addFlashAttribute("typeId",typeId);

        return "redirect:/admin/addtype";
    }

    @PostMapping(value = "/updatetypetwo")
    public String updatetypetwo(String name,Long typeId , HttpSession session,RedirectAttributes redirectAttributes)
    {

        UpdateWrapper<Type> typeUpdateWrapper = new UpdateWrapper<>();
        QueryWrapper<Type> typeQueryWrapper = new QueryWrapper<>();

        Long userId = (Long) session.getAttribute("id");

        typeQueryWrapper.eq("name",name).eq("user_id",userId);

        Integer integer = typeMapper.selectCount(typeQueryWrapper);

        if ( integer != 0)
        {
            redirectAttributes.addFlashAttribute("message","分类已存在了");
            System.out.println("daozgl");
            redirectAttributes.addFlashAttribute("name",name);
            redirectAttributes.addFlashAttribute("typeId",typeId);
            return "redirect:/admin/addtype";
        }


        typeUpdateWrapper.set("name",name).eq("user_id",userId).eq("id",typeId);

        int update = typeMapper.update(null, typeUpdateWrapper);

        if (update == 0)
        {
            redirectAttributes.addFlashAttribute("message","修改失败");
            return "redirect:/admin/addtype";
        }

        redirectAttributes.addFlashAttribute("message","修改成功");

        return "redirect:/admin/type";

    }
}

